package cn.jinjimi.app.main.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.jinjimi.app.R;
import cn.jinjimi.app.main.ProductSearchPresenter;
import cn.jinjimi.app.main.adapter.ProductAdapter;
import cn.jinjimi.app.main.extend.BaseActivity;
import cn.jinjimi.app.main.widget.XListView.XListView;
import cn.jinjimi.app.util.SearchRecordManager;
import cn.jinjimi.app.bean.SharePrefKeys;
import cn.jinjimi.base.util.AndroidUtils;

/**
 * @author zhenhua
 */
public class ProductSearchActivity extends BaseActivity implements View.OnClickListener,
        XListView.IXListViewListener, AdapterView.OnItemClickListener, ProductSearchPresenter.IViewController {

    private EditText etSearch;
    private LinearLayout llSearchOptions;
    private ProductAdapter mAdapter;
    private SearchRecordManager mSearchManager =
            new SearchRecordManager(SharePrefKeys.PREF_ZDB_PRODUCT_SEARCH_HISTORY, 5);

    @Override
    protected void beforeInitView() {
        setContentView(R.layout.activity_product_search);
    }

    @Override
    protected void initial() {
        initSearchInputView();
        initSearchOptionView();
        initSearchResultView();
        initData();
    }

    private void initSearchResultView() {
        XListView listView = (XListView) findViewById(R.id.listview);
        listView.setPullLoadEnable(false);
        listView.setPullRefreshEnable(false);
        listView.setTransparentBg();
        listView.setXListViewListener(this);
        listView.setTransparentBg();

        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    AndroidUtils.hideSoftKeyBoard(ProductSearchActivity.this, etSearch);
                }
                return false;
            }
        });

        listView.setOnItemClickListener(this);
        listView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    AndroidUtils.hideSoftKeyBoard(ProductSearchActivity.this, etSearch);
                }
                return false;
            }
        });

        mAdapter = new ProductAdapter(this);
        listView.setAdapter(mAdapter);
    }

    private void initSearchInputView() {
        etSearch = (EditText) findViewById(R.id.et_search);
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 0) {
                    mAdapter.clearItems();
                    llSearchOptions.setVisibility(View.VISIBLE);
                    findViewById(R.id.tv_search_result).setVisibility(View.GONE);
                }
            }
        });

        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                return false;
            }
        });

        ImageView imgClear = (ImageView) findViewById(R.id.img_clear);
        imgClear.setOnClickListener(this);

        TextView tvCancel = (TextView) findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(this);
    }

    private void initSearchOptionView() {
        llSearchOptions = (LinearLayout) findViewById(R.id.ll_search_options);
        RecyclerView searchConfigView = (RecyclerView) findViewById(R.id.search_config_recycler_view);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        searchConfigView.setLayoutManager(linearLayoutManager);
    }

    private void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                onCancelAction();
                break;
            case R.id.img_clear:
                onClearInputAction();
                break;
            default:break;
        }
    }

    private void onClickRecentKeywordAction(String tag) {

    }

    private void onCancelAction() {
        finish();
    }

    private void onClearInputAction() {
        if (etSearch != null) {
            etSearch.setText("");
        }
    }

    @Override
    public void onRefresh() {
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
